package com.rp.demo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.rp.demo.commons.Constant;
import com.rp.demo.util.JodaTimeUtil;
import com.rp.demo.util.JsonUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * 消息投递日志
 *
 * @author ruipeng
 * @since 2021-05-07
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MsgLog implements Serializable {

    @TableId(value = "msg_id", type = IdType.INPUT)
    private String msgId;

    private String msg;

    //"交换机不能为空")
    private String exchange;

    //"路由键不能为空")
    private String routingKey;

    //"状态: 0投递中 1投递成功 2投递失败 3已消费")
    private Integer status;

    private Integer tryCount;

    private Date nextTryTime;

    private Date createTime;

    private Date updateTime;


    public MsgLog(String msgId, Object msg, String exchange, String routingKey) {
        this.msgId = msgId;
        this.msg = JsonUtil.objToStr(msg);
        this.exchange = exchange;
        this.routingKey = routingKey;

        this.status = Constant.MsgLogStatus.DELIVERING;
        this.tryCount = 0;

        Date date = new Date();
        this.createTime = date;
        this.updateTime = date;
        this.nextTryTime = (JodaTimeUtil.plusMinutes(date, 2));
    }

}
